package com.lhh.algorithmservice.scheduled;

import com.lhh.algorithmservice.service.CalculateRatingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 李厚航
 */
@Component
@Slf4j
public class RecommendationScheduled {

    private final CalculateRatingService calculateRatingService;

    @Autowired
    public RecommendationScheduled(CalculateRatingService calculateRatingService) {
        this.calculateRatingService = calculateRatingService;
    }

    /**
     * 生成评分表并训练ALS模型
     */
    @Scheduled(fixedDelay = 1000*60*60*24,initialDelay = 0)
    @Async
    public void generateRatingCsv() {
        long startTime = System.currentTimeMillis();
        log.info("开始生成评分表...");
        calculateRatingService.generateRatingCsv();
        long endTime = System.currentTimeMillis();
        log.info("生成评分表成功,耗时" + (endTime - startTime) + "ms");
        startTime = System.currentTimeMillis();
        log.info("开始训练ALS模型...");
        calculateRatingService.trainALSModel();
        endTime = System.currentTimeMillis();
        log.info("训练ALS模型成功，耗时" + (endTime - startTime) + "ms");
    }
}
